package com.example.administrator.liangcang.message;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.liangcang.BR;
import com.example.administrator.liangcang.R;
import com.example.administrator.liangcang.callback.LiangCangService;
import com.example.administrator.liangcang.databinding.FragmentAssortmentBinding;
import com.example.administrator.liangcang.message.bean.AssormentFragmentEntity;
import com.example.administrator.liangcang.message.bean.AssormentFragmentItem;
import com.example.administrator.liangcang.until.BaseApplication;
import com.example.administrator.liangcang.until.CommentRecyclerAdapter;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class AssortmentFragment extends Fragment implements Callback<AssormentFragmentEntity> {
   private FragmentAssortmentBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private LiangCangService service;

    public AssortmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_assortment, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       binding = DataBindingUtil.getBinding(view);
        aboutRecycler();
    }

    private void aboutRecycler() {
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        binding.recycler.setLayoutManager(manager);
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), R.layout.assortment_fragment_item, BR.assormentFragmentItem);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getAssormentFragmentEntity().enqueue(this);
    }

    @Override
    public void onResponse(Call<AssormentFragmentEntity> call, Response<AssormentFragmentEntity> response) {
        AssormentFragmentItem item = new AssormentFragmentItem();
        item.setCat_name("我的收藏");
        item.setId(0);
        adapter.add(0,item);
        AssormentFragmentItem item2 = new AssormentFragmentItem();
        item2.setCat_name("所有杂志");
        item2.setId(1);
        adapter.add(1,item2);
        adapter.addAll(response.body().getData().getItems());
    }

    @Override
    public void onFailure(Call<AssormentFragmentEntity> call, Throwable t) {
        Toast.makeText(getContext(), "assortmentFragment"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
